package meta.api.web.value.business;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import meta.api.web.value.dao.ValueMngDao;
import meta.common.util.JsonUtil;
import meta.common.util.ListUtil;
import meta.common.util.StrUtil;

@Component
public class ValueMngService {

    @Autowired
    private ValueMngDao valueMngDao;

    public int getTotal(String type, List<String> keys) {
        if (ListUtil.isEmptyList(keys)) {
            return 0;
        }
        return valueMngDao.getTotal(type, keys);
    }

    public List<Object> getList(String type, List<String> keys,
            Integer current, Integer pageSize) {
        if (ListUtil.isEmptyList(keys)) {
            return null;
        }
        List<Object> result = new ArrayList<Object>();
        List<String> valueList =  valueMngDao.getList(type, keys, current, pageSize);
        if (ListUtil.isNotEmptyList(valueList)) {
            for (String value : valueList) {
                result.add(JsonUtil.toObj(value, Object.class));
            }
        }
        return result;
    }

    public Object getValue(String type, String key) {
        String value =  valueMngDao.getValue(type, key);
        if (StrUtil.isNotEmpty(value)) {
            return JsonUtil.toObj(value, Object.class);
        } else {
            return null;
        }
    }
}
